﻿using System;
using Enyim.Caching;
using Ayatta.Contract;
using System.Web.Mvc;
using Web.Search.Models;
using Ayatta.Common;
using Ayatta.Contract.Component;
using Ayatta.Contract.Domain;
using Web.Core.Models;
using Web.Core.Extensions;

namespace Web.Main.Controllers
{
    public class HomeController : BaseController
    {
        private static readonly string ProductIndexService = "ProductIndexService";
        public HomeController(Lazy<IInfrastructureService> infrastructure, Lazy<IPassportService> passport, Lazy<IAccountService> account, Lazy<IStoreService> store, Lazy<ITradeService> trade, Lazy<IPlayService> play, Lazy<IMiscService> misc, Lazy<IExtraService> extra, IMemcachedClient memcachedClient)
            : base(infrastructure, passport, account, store, trade, play, misc, extra, memcachedClient)
        {
        }

        public ActionResult Index(string keyword, int page = 1, int size = 20, string brand = null, string cate = null, string prop = null, bool create = false)
        {
            var model = new HomeControllerModel.Index();
            var proxy = new ServiceClientProxy<IProductIndexService>(ProductIndexService);
            if (create)
            {
                proxy.Invoke(o => o.Index());
            }
            model.Title = "搜索";

            var param = new ProductQueryParam();
            param.Keyword = keyword;
            param.Highlight = true;
            model.Items = proxy.Invoke(o => o.Search(param)).ToPagedList<ProductItem, ProductQueryStatistics>(page, size);
            return View(model);
        }

        public ActionResult Participle(string input)
        {
            var model = new ViewModel<string[]>();
            var proxy = new ServiceClientProxy<IProductIndexService>(ProductIndexService);

            model.Title = "分词";
            if (!string.IsNullOrEmpty(input))
            {
                model.Data = proxy.Invoke(o => o.Participle(input));
            }
            return View(model);
        }
    }
}
